package com.study.创造型模式.工厂模式.抽象工厂模式.消费者;

import com.study.创造型模式.工厂模式.抽象工厂模式.生产者.AbsFactory;
import com.study.创造型模式.工厂模式.抽象工厂模式.生产者.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizza {

    private AbsFactory factory;


    public OrderPizza(AbsFactory factory) {
        setFactory(factory);
    }

    private void setFactory(AbsFactory factory) {
        Pizza pizza = null;
        String orderType = "";  //获取用户输入的披萨类型
        this.factory = factory;//设置简单工厂对象
        do {
            orderType = getType();
            pizza = factory.createPizza(orderType);
            //输出pizza
            if (pizza != null) {
                //输出pizza 制作过程
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
                System.out.println(pizza.name + "订购成功");
            } else {
                System.out.println("订购披萨失败");
                break;
            }
        } while (true);
    }

    //写一个方法，可以获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入披萨种类");
            String str = br.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
